/*     */ package com.zimbra.cs.store.http;
/*     */ 
/*     */ import com.zimbra.common.httpclient.HttpClientUtil;
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.common.util.ByteUtil;
/*     */ import com.zimbra.common.util.ByteUtil.PositionInputStream;
/*     */ import com.zimbra.common.util.ZimbraHttpConnectionManager;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import com.zimbra.cs.service.UserServlet.HttpInputStream;
/*     */ import com.zimbra.cs.store.external.ExternalStoreManager;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.security.DigestInputStream;
/*     */ import java.security.MessageDigest;
/*     */ import java.security.NoSuchAlgorithmException;
/*     */ import org.apache.commons.httpclient.HttpClient;
/*     */ import org.apache.commons.httpclient.methods.DeleteMethod;
/*     */ import org.apache.commons.httpclient.methods.GetMethod;
/*     */ import org.apache.commons.httpclient.methods.PostMethod;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public abstract class HttpStoreManager
/*     */   extends ExternalStoreManager
/*     */ {
/*     */   protected abstract String getPostUrl(Mailbox paramMailbox);
/*     */   
/*     */   protected abstract String getGetUrl(Mailbox paramMailbox, String paramString);
/*     */   
/*     */   protected abstract String getDeleteUrl(Mailbox paramMailbox, String paramString);
/*     */   
/*     */   protected abstract String getLocator(PostMethod paramPostMethod, String paramString, long paramLong, Mailbox paramMailbox)
/*     */     throws ServiceException, IOException;
/*     */   
/*     */   public String writeStreamToStore(InputStream in, long actualSize, Mailbox mbox)
/*     */     throws IOException, ServiceException
/*     */   {
/*     */     MessageDigest digest;
/*     */     try
/*     */     {
/*  51 */       digest = MessageDigest.getInstance("SHA-256");
/*     */     } catch (NoSuchAlgorithmException e) {
/*  53 */       throw ServiceException.FAILURE("SHA-256 digest not found", e);
/*     */     }
/*  55 */     ByteUtil.PositionInputStream pin = new ByteUtil.PositionInputStream(new DigestInputStream(in, digest));
/*     */     
/*  57 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  58 */     PostMethod post = new PostMethod(getPostUrl(mbox));
/*     */     try {
/*  60 */       HttpClientUtil.addInputStreamToHttpMethod(post, pin, actualSize, "application/octet-stream");
/*  61 */       int statusCode = HttpClientUtil.executeMethod(client, post);
/*  62 */       if ((statusCode == 200) || (statusCode == 201) || (statusCode == 204)) {
/*  63 */         return getLocator(post, ByteUtil.encodeFSSafeBase64(digest.digest()), pin.getPosition(), mbox);
/*     */       }
/*  65 */       throw ServiceException.FAILURE("error POSTing blob: " + post.getStatusText(), null);
/*     */     }
/*     */     finally {
/*  68 */       post.releaseConnection();
/*     */     }
/*     */   }
/*     */   
/*     */   public InputStream readStreamFromStore(String locator, Mailbox mbox)
/*     */     throws IOException
/*     */   {
/*  75 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  76 */     GetMethod get = new GetMethod(getGetUrl(mbox, locator));
/*  77 */     int statusCode = HttpClientUtil.executeMethod(client, get);
/*  78 */     if (statusCode == 200) {
/*  79 */       return new UserServlet.HttpInputStream(get);
/*     */     }
/*  81 */     get.releaseConnection();
/*  82 */     throw new IOException("unexpected return code during blob GET: " + get.getStatusText());
/*     */   }
/*     */   
/*     */ 
/*     */   public boolean deleteFromStore(String locator, Mailbox mbox)
/*     */     throws IOException
/*     */   {
/*  89 */     HttpClient client = ZimbraHttpConnectionManager.getInternalHttpConnMgr().newHttpClient();
/*  90 */     DeleteMethod delete = new DeleteMethod(getDeleteUrl(mbox, locator));
/*     */     try {
/*  92 */       int statusCode = HttpClientUtil.executeMethod(client, delete);
/*  93 */       boolean bool; if ((statusCode == 200) || (statusCode == 204))
/*  94 */         return true;
/*  95 */       if (statusCode == 404) {
/*  96 */         return false;
/*     */       }
/*  98 */       throw new IOException("unexpected return code during blob DELETE: " + delete.getStatusText());
/*     */     }
/*     */     finally {
/* 101 */       delete.releaseConnection();
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/store/http/HttpStoreManager.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */